//
//  ViewController.m
//  XJProgressHUD
//
//  Created by 石显军 on 2019/12/16.
//  Copyright © 2019 石显军. All rights reserved.
//

#import "ViewController.h"
#import "UIViewController+XJHud.h"

@interface ViewController ()

@property (nonatomic, assign) CGFloat hudProgress;

@property (nonatomic, strong) dispatch_source_t timer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark - Action

- (IBAction)loadingOnclick:(id)sender
{
    [self xj_showLoading];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self xj_dismiss];
    });
}

- (IBAction)successOnclick:(id)sender
{
    [self xj_showSuccessMessage:@"添加成功"];
}

- (IBAction)errorOnclick:(id)sender
{
    [self xj_showErrorMessage:@"添加失败"];
}

- (IBAction)uploadOnlick:(id)sender
{
    self.hudProgress = 0;
    [self xj_showProgressMessage:@"上传中..."];
    
    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
    dispatch_source_set_timer(self.timer, DISPATCH_TIME_NOW, 0.001 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(self.timer, ^{
        if (self.hudProgress >= 1) {
            dispatch_cancel(self.timer);
            [self xj_showSuccessMessage:@"上传成功"];
        }else{
            self.hudProgress += 0.001;
            [self xj_updateProgressWithValue:self.hudProgress];
        }
    });
    dispatch_resume(self.timer);
}

- (IBAction)topOnclick:(id)sender
{
    [self xj_showTopMessage:@"顶部提示"];
}

- (IBAction)centerOnclick:(id)sender
{
    [self xj_showCenterMessage:@"中间提示"];
}
- (IBAction)bottomOnclick:(id)sender
{
    [self xj_showBottomMessage:@"底部提示"];
}


@end
